<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin', 'admin', 0);
pc_base::load_sys_class('form', '', 0);

class manage extends admin {
	private $db;
	private $audit_db;
	private $url_source_db;
	public $siteid;
	
	function __construct() {
	    parent::__construct();
	    pc_base::load_app_class('global');
	    $this->db = pc_base::load_model('user_info_model');//加载model
	    $this->admin_db = pc_base::load_model('admin_model');
	    $this->audit_db = pc_base::load_model('audit_model');
	    $this->url_source_db = pc_base::load_model('url_source_log_model');
	    $this->talent_db = pc_base::load_model('talent_model');
	    $this->talent_source = pc_base::load_model('talent_source_model');//加载人才库关联资源模型
	    $this->picture_db = pc_base::load_model('picture_nsr_model');//加载图集模型
	    $this->siteid = get_siteid();//获取当前站点
	    $this->admin_userid = param::get_cookie('userid'); // 获取当前登录的管理员ID
	}
	
	public function init() {
		$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
		$where = "";
		$where.= " status = 1 ";
		//注册时间
		if ($_GET['begin_time'] || $_GET['end_time']) {
		    $begin_time = $_GET['begin_time'];
			$end_time = $_GET['end_time'];
		    if($begin_time && $end_time){
		        $where.= " and create_time >= '$begin_time'"." and create_time <='$end_time'";
		    }else{
		        if($begin_time){
		            $where.= " and create_time >= '$begin_time'";
		        }
		        if($end_time){
		            $where.= " and create_time <= '$end_time'";
		        }
		    }
		}
		if ($_GET['nickname'] || $_GET['tel']) {
			if (!empty($_GET['tel'])) {
				$tel = $_GET['tel'];
				$where.= " and tel = '$tel'";
			}
			if ($_GET['nickname']) {
				$nickname = $_GET['nickname'];
				$where.= " and nickname like '%".$nickname."%'";
			}
			$user_info = $this->db->listinfo($where,'id DESC',$page,10);
		} else {
			$user_info = $this->db->listinfo($where,'id DESC',$page,10);
		}
		$username = array();
		foreach ($user_info as $k=>$v){
			$username = $this->admin_db->get_one(array('userid'=>$v['updated_by']),'realname');
			$user_info[$k]['username'] = $username['realname'];
		}
		$pages = $this->db->pages;
		pc_base::load_sys_class('format', '', 0);
		include $this->admin_tpl('user_info_list');
	}
	
	public function detail() {
	    $id = isset($_GET['id']) ? intval($_GET['id']) : showmessage(L('message not exits'),HTTP_REFERER);
	    $info = $this->db->get_one(array('id'=>$id));
		$username = $this->admin_db->get_one(array('userid'=>$info['updated_by']),'realname');
		$info['username'] = $username['realname'];
	    $show_header = false;
	    pc_base::load_sys_class('form', '', 0);
	    include $this->admin_tpl('user_info_detail');
	}
	/**
	 * 认证待审核列表
	 */
	public function audit_list(){
		$page = $_GET['page'] ? intval($_GET['page']) : 1;
		$where = "";
		$search_info = $_GET['audit'];
		if ($search_info) {
			if ($search_info['name']) {
				$name = $search_info['name'];
				$where .= " name LIKE '%$name%' and ";
			}
			if ($search_info['tel']) {
				$where .= "tel=".$search_info['tel']." and ";
			}
		}
		$where .= "user_type in (2,3,4)";
		$info = $this->audit_db->listinfo($where,'id DESC',$page,10);
		$pages = $this->audit_db->pages;
		include $this->admin_tpl('audit_list');
	}
	
	public function check_log(){
		$id = $_GET['id'];
		$show_header = false;
		include $this->admin_tpl('audit_check_log');
	}
	
	/**
	 * 认证待审核详情
	 */
	public function audit_detail() {
		$id = isset($_GET['id']) ? intval($_GET['id']) : showmessage(L('message not exits'),HTTP_REFERER);
		$info = $this->audit_db->get_one(array('id'=>$id));
		if (!empty($info['model_imgs'])) {
			$info['model_imgs'] = explode(',', $info['model_imgs']);
		}
		$userInfo = $this->db->getUsername();
		$adminInfo = $this->admin_db->getRealnameInfo();
		$show_header = false;
		include $this->admin_tpl('audit_detail');
	}
	
	/**
	 * 认证审核
	 */
	public function audit_check() {
		$id = $_POST['check_id'];
// 		$id = explode('|', $_POST['check_id']);
// 		$where = "";
// 		foreach ($id as $k=>$v){
// 			if (intval($v)) {
// 				$id[$k] = intval($v);
// 			} else {
// 				unset($id[$k]);
// 			}
// 		}
// 		$id = implode('\',\'', $id);
// 		$where .= "id in ('$id')";
// 		$info = $this->audit_db->select($where);
		$info = $this->audit_db->get_one(array('id'=>$id));
		//记录行为日志
		$log_content = "";
// 		foreach ($info as $ks=>$vs){
			if ($_POST['check_status'] == 2) {//认证驳回
				if (empty($_POST['audit_opinion']) || $_POST['audit_opinion'] == "") {
					showmessage(L('audit_opinion_not_empty'),HTTP_REFERER);
				}
				$user_data['authentication_status'] = 3;
				$check_info="驳回";
			} elseif($_POST['check_status'] == 3) {//认证通过
				$check_info="通过";
				$talent_id = $this->insert_into_talent($info);
				if (!$talent_id) {
					showmessage(L('error'),HTTP_REFERER);
				}
// 				$this->refreshData(6);
				$data['talent_id'] = $talent_id;
				$user_data['is_authentication'] = 1;
				$user_data['talent_id'] = $talent_id;
				$user_data['authentication_status'] = 2;
				$user_data['type'] = $info['user_type'];
			}
			$data['status'] = $_POST['check_status'];
			$data['audit_opinion'] = $_POST['audit_opinion'] ? $_POST['audit_opinion'] : '通过';
			$data['audit_time'] = date('Y-m-d H:i:s',time());
			$data['audit_user'] = $this->admin_userid;
			$apply_type = $info['apply_type'] ? intval($info['apply_type']) : 1;
			if ($apply_type == 1) {
				$user_result = $this->db->update($user_data,array('id'=>$info['created_by']));//更新用户表的认证状态
				if (!$user_result) {
					showmessage(L('error'),HTTP_REFERER);
				}
// 				$this->refreshData(13);//认证驳回刷新缓存
			}
			$user_result = $user_result ? $user_result : true;
			$result = $this->audit_db->update($data,"id in ('$id')");//更新认证表的认证人的审核状态
			$log_content .= '<span style="color:blue;">认证审核</span>[姓名：'.$info['name'].';审核状态：'.$check_info.';]<br>';
// 		}
		if ($result && $user_result) {
			$this->manage_admin_log($log_content);
			$result_info = array();
			$result_info['status'] = 200;
			$result_info['info'] = 'SUCCESS';
			echo json_encode($result_info);
			exit();
// 			showmessage(L('success'),'?m=user_info&c=manage','','check');
		} else {
			$result_info = array();
			$result_info['status'] = 201;
			$result_info['info'] = 'ERROR';
			echo json_encode($result_info);
			exit();
// 			showmessage(L('error'),HTTP_REFERER);
		}
	}
	
	/**
	 * 认证审核通过将信息写入人才库
	 */
	public function insert_into_talent($param){
		$data = array();
		$data['real_name'] = $param['name'];
		$data['gender'] = $param['gender'];
		$data['height'] = $param['height'];
		$data['weight'] = $param['weight'];
		$data['bust'] = $param['bust'];
		$data['waist'] = $param['waist'];
		$data['hips'] = $param['hips'];
		$data['telephone'] = $param['tel'];
		$data['birthday'] = $param['birthday'];
		$data['province_native'] = $param['province_native'];
		$data['identification'] = $param['id_number'];
		if ($param['user_type'] == 2) {
			$data['is_moter'] = 1;
		} elseif ($param['user_type'] == 3) {
			$data['is_agent'] = 1;
		} elseif ($param['user_type'] == 4) {
			$data['is_business'] = 1;
		}
		$data['follow'] = rand(500,1000);
		$data['praise'] = rand(500,1000);
		$data['create_time'] = $data['update_time'] = date("Y-m-d H:i:s",time());
		$data['created_by'] = $data['updated_by'] = $this->admin_userid;
		$data['source'] = '2-'.$param['id'];
		$data['company'] = $param['company_name'];
		$data['model_card_img'] = $param['model_card_img'];//模卡
		$data['model_card_imgs'] = $param['model_imgs'];//模卡组图
		$data['id_card_front_img'] = $param['id_card_front_img'];
		$data['id_card_back_img'] = $param['id_card_back_img'];
		$data['hold_id_card_img'] = $param['hold_id_card_img'];
		$data['license_or_card_img'] = $param['license_or_card_img'];
		$data['model_card_img'] = $param['model_card_img'];
		$data['image_background'] = '/resource/uploadfile/images/talentBackground/talentBg.jpg';//默认人才背景图 
		$talent_result = $this->talent_db->insert($data,true);
		//认证图集信息不为空，写入图集信息
		/* if(isset($param['model_imgs']) && !empty($param['model_imgs'])){
			$pictrue_info = array_filter(explode(',', $param['model_imgs']));
			$pictrueurl = array();
			foreach ($pictrue_info as $k=>$v){
				$pictrueurl[$k]['url'] = $v;
				$pictrueurl[$k]['alt'] = '';
			}
			$picture_data = array();
			$picture_data['title'] = $param['name'].'图集';
			$picture_data['source_name'] = '新丝路时尚';
			$picture_data['image_cover'] = $pictrue_info[0];
			$picture_result = $this->picture_db->check($picture_data,'add');
			$picture_result['pictureurls'] = json_encode($pictrueurl,JSON_UNESCAPED_SLASHES);
			$picture_result['source'] = 1;
			$picture_id = $this->picture_db->edit($picture_result);//图集信息写入图集表
		} */
		/* if (isset($talent_result) && isset($picture_id)) {
			$source = array();
			$source['source_id'] = $picture_id;
			$source['source_type'] = 3;
			$source['talent_id'] = $talent_result;
			$source['create_time'] = $source['update_time'] = date('Y-m-d H:i:s',time());
			$source['created_by'] = $source['updated_by'] = $this->admin_userid;
			$source_result = $this->talent_source->insert($source);
		} */
		return $talent_result;
	}
	
	//链接点击统计 2017-11-16 09:39:08 吴发顶
	public function url_source_count() {
		$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
		$where = " id >=1";
		//注册时间
		if ($_GET['begin_time'] || $_GET['end_time']) {
		    $begin_time = $_GET['begin_time'];
			$end_time = $_GET['end_time'];
		    if($begin_time && $end_time){
		        $where.= " and create_time >= '$begin_time'"." and create_time <='$end_time'";
		    }else{
		        if($begin_time){
		            $where.= " and create_time >= '$begin_time'";
		        }
		        if($end_time){
		            $where.= " and create_time <= '$end_time'";
		        }
		    }
		}
		if ($_GET['source'] || $_GET['ip']) {
			if (!empty($_GET['ip'])) {
				$ip = $_GET['ip'];
				$where.= " and ip = '$ip'";
			}
			if ($_GET['source']) {
				$source = $_GET['source'];
				$where.= " and source like '%".$source."%'";
			}
			$url_source = $this->url_source_db->listinfo($where,'id DESC',$page,10);
		} else {
			$url_source = $this->url_source_db->listinfo($where,'id DESC',$page,10);
		}
		$pages = $this->url_source_db->pages;
		pc_base::load_sys_class('format', '', 0);
		include $this->admin_tpl('url_source_list');
	}
	
	/**
	 * 认证审核成功刷新缓存
	 */
	private function refreshData($type) {
		$result = file_get_contents(API_URL.'/xinsilu/v1/refreshRedis/refreshRedis?type='.$type);
		$data = json_decode($result,true);
		return $data;
	}
	
	/**
	 * 认证审核详情页移除模卡组图
	 */
	public function remove_model_img(){
		$res = array();
		$src = $_POST['src'];
		$id = $_POST['id'];
		if (!isset($id) || !isset($src)) {
			$res['status'] = '202';
			$res['msg'] = 'missing parameter';
			echo json_encode($res);
			exit();
		}
		$info = $this->audit_db->get_one(array('id'=>$id),'name,model_imgs');
		$data = str_replace($src, '', $info['model_imgs']);//组图中如果包含要移除的图片路径则替换为空
		$new_data = array_filter(explode(',', $data));//删除空数组
		$model_imgs = implode(',', $new_data);
		$result = $this->audit_db->update(array('model_imgs'=>$model_imgs),array('id'=>$id));
		if ($result) {
			//记录行为日志
			$log_content = "";
			$log_content .= '<span style="color:red;">移除模卡组图中指定图片</span>[模特姓名：'.$info['name'].'，图片地址：'.$src.']<br>';
			$this->manage_admin_log($log_content);
			$res['status'] = '200';
			$res['msg'] = 'SUCESS';
			echo json_encode($res);
			exit();
		} else {
			$res['status'] = '201';
			$res['msg'] = 'ERROR';
			echo json_encode($res);
			exit();
		}
	}
	
	/**
	 * 认证审核列表刷新缓存（刷新多个缓存,英文逗号隔开）
	 */
	public function refresh_cache() {
		$type = $_POST['type'];
		$result = array();
		if (strpos($type, ',')) {
			$type_arr = explode(',', $type);
			foreach ($type_arr as $v){
				$result[$v] = $this->refreshData($v);
			}
			foreach ($result as $k=>$v){
				if ($v['code']!='R0000000') {
					$res['code'] = '201';
					$res['msg'] = '缓存类型为'.$k.'的接口刷新数据失败！';
					echo json_encode($res);
					exit();
				}
			}
			$res['code'] = '200';
			$res['msg'] = '成功';
			echo json_encode($res);
			exit();
		} else {
			$result = $this->refreshData($type);
			if ($result['code'] == 'R0000000') {
				$res['code'] = '200';
				$res['msg'] = '成功';
				echo json_encode($res);
				exit();
			} else {
				$res['code'] = '201';
				$res['msg'] = '缓存类型为'.$type.'的接口刷新数据失败！';
				echo json_encode($res);
				exit();
			}
		}
	}
}